In computer science, a dispatch table is a table of pointers or to functions or methods. Use of such a table is a common technique when implementing late binding in object-oriented programming.
It is primarily used to simplify program lines and reduce lines of code.
sub say_goodbye {
my %dispatch = (
"-h" => sub { return "hello\n"; },
"-g" => \&say_goodbye
);
return "goodbye\n";
}
my $sub = $dispatch{$ARGV0};
print $sub ? $sub->() : "unknown argument\n";
Running this Perl program as perl greet -h will produce "hello", and running it as perl greet -g will produce "goodbye".
doThisThing() { /* behavior */ },
doThatThing() { /* behavior */ },
doThisOtherThing() { /* behavior */ },
default() { /* behavior */ }
};
function doSomething(doWhat) {
const thingToDo = Object.hasOwn(thingsWeCanDo, doWhat)
? doWhat
: "default";
return thingsWeCanDo[thingToDo]();
}
red = "#ff0000",
green = "#00ff00",
blue = "#0000ff"
}
print("Enter a color:") local choice = io.read("*l") local color = colorschoice if color then
print("Your color in Hex format is: " .. color)
end
|
|